package com.youlai.boot.system.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.system.mapper.LpsFlMapper;
import com.youlai.boot.system.service.LpsFlService;
import com.youlai.boot.system.model.entity.LpsFl;
import com.youlai.boot.system.model.form.LpsFlForm;
import com.youlai.boot.system.model.query.LpsFlQuery;
import com.youlai.boot.system.model.vo.LpsFlVO;
import com.youlai.boot.system.converter.LpsFlConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 订单分类服务实现类
 *
 * @author lwb
 * @since 2025-07-16 21:51
 */
@Service
@RequiredArgsConstructor
public class LpsFlServiceImpl extends ServiceImpl<LpsFlMapper, LpsFl> implements LpsFlService {

    private final LpsFlConverter lpsFlConverter;

    /**
    * 获取订单分类分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<LpsFlVO>} 订单分类分页列表
    */
    @Override
    public IPage<LpsFlVO> getLpsFlPage(LpsFlQuery queryParams) {
        Page<LpsFlVO> pageVO = this.baseMapper.getLpsFlPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取订单分类表单数据
     *
     * @param id 订单分类ID
     * @return
     */
    @Override
    public LpsFlForm getLpsFlFormData(Long id) {
        LpsFl entity = this.getById(id);
        return lpsFlConverter.toForm(entity);
    }
    
    /**
     * 新增订单分类
     *
     * @param formData 订单分类表单对象
     * @return
     */
    @Override
    public boolean saveLpsFl(LpsFlForm formData) {
        LpsFl entity = lpsFlConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新订单分类
     *
     * @param id   订单分类ID
     * @param formData 订单分类表单对象
     * @return
     */
    @Override
    public boolean updateLpsFl(Long id,LpsFlForm formData) {
        LpsFl entity = lpsFlConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除订单分类
     *
     * @param ids 订单分类ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteLpsFls(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的订单分类数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
